﻿<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="../helpproject.xsl" ?>
<topic template="Default" lasteditedby="Geert" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../helpproject.xsd">
  <title translate="true">Catel.Extensions.FluentValidation</title>
  <keywords>
    <keyword translate="true">FluentValidation</keyword>
    <keyword translate="true">IValidationProvider</keyword>
  </keywords>
  <body>
    <header>
      <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Catel.Extensions.FluentValidation</text></para>
    </header>
    <para><br/><text styleclass="Normal" translate="true">The validation in Catel is extremely flexible, at this point you must already know it, but sometimes it is just not enough or you are forced to use external validators.</text></para>
    <para></para>
    <para><link displaytype="text" defaultstyle="true" type="weblink" href="http://fluentvalidation.codeplex.com" target="_blank" styleclass="Normal" style="font-style:normal;" translate="true">FluentValidation</link><text styleclass="Normal" translate="true"> is a small validation library for .NET that uses a fluent interface and lambda expressions for building validation rules for your business objects. Catel provides an extension in order to use </text><link displaytype="text" defaultstyle="true" type="weblink" href="http://fluentvalidation.codeplex.com" target="_blank" styleclass="Normal" style="font-style:normal;" translate="true">FluentValidation</link><text styleclass="Normal" translate="true"> as a validation library.</text></para>
    <para styleclass="Normal"><br/><text styleclass="Normal" translate="true">The only thing you have to do is install an isolate package named </text><text styleclass="Normal" style="font-style:italic;" translate="true">Catel.Extensions.FluentValidation</text><text styleclass="Normal" translate="true">, available via </text><link displaytype="text" defaultstyle="true" type="weblink" href="http://www.nuget.org" target="_blank" styleclass="Normal" translate="true">NuGet</link><text styleclass="Normal" translate="true">, then you will be able to write your view models validations using </text><link displaytype="text" defaultstyle="true" type="weblink" href="http://fluentvalidation.codeplex.com" target="_blank" styleclass="Normal" translate="true">FluentValidation</link><text styleclass="Normal" translate="true"> approach.</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">public</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">class</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">PersonViewModelValidator</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">:</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">AbstractValidator&lt;PersonViewModel&gt;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> &#160; &#160;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">public</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">PersonViewModelValidator()</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> &#160; &#160;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> &#160; &#160; &#160; &#160;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">RuleFor(person</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">=&gt;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">person.FirstName).NotEmpty();</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> &#160; &#160; &#160; &#160;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">RuleFor(person</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">=&gt;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">person.LastName).NotEmpty().WithMessage(&quot;Please specify the last name&quot;);</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true"> &#160; &#160;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">}</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">}</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">In order to retrieve the right validators, you must register the </text><text styleclass="Normal" style="font-style:italic;" translate="true">FluentValidatorProvider</text><text styleclass="Normal" translate="true">:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">ServiceLocator.Instance.RegisterType&lt;IValidatorProvider,</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">FluentValidatorProvider&gt;();</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">The </text><text styleclass="Normal" style="font-style:italic;" translate="true">FluentValidatorProvider</text><text styleclass="Normal" translate="true"> will automatically retrieve the right validators associated with the view models.</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">How handle all Catel validation concepts with fluent validation classes?</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Catel handle concepts like field or business rules errors and warnings . So, it&apos;s necessary map the fluent validation class to the specific Catel validation using ValidationDescriptionAttribute.</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" translate="true">[ValidationDescription(Tag = &quot;Person&quot;, ValidationResultType = ValidationResultType.Error, ValidationType = ValidationType.Field)]</text><br/><text styleclass="Code Example" style="font-weight:bold;" translate="true">public</text><text styleclass="Code Example" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:bold;" translate="true">class</text><text styleclass="Code Example" translate="true"> PersonViewModelValidator : AbstractValidator&lt;PersonViewModel&gt;</text><br/><text styleclass="Code Example" translate="true">{</text><br/><text styleclass="Code Example" translate="true">}</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">How FluentValidationProvider works?</text></para>
    <para styleclass="Normal"><text styleclass="Normal" style="font-style:italic;" translate="true">FluentValidationProvider</text><text styleclass="Normal" translate="true"> is an implementation of </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_Data_IValidatorProvider" styleclass="Normal" style="font-style:normal;" translate="true">IValidationProvider</link><text styleclass="Normal" translate="true"> (see: </text><link displaytype="text" defaultstyle="true" type="topiclink" href="Validation_via_IValidator" styleclass="Normal" style="font-style:normal;" translate="true">Validation via IValidator</link><text styleclass="Normal" translate="true">). It search all validators classes that implements </text><text styleclass="Normal" style="font-style:italic;" translate="true">FluentValidation.IValidator</text><text styleclass="Normal" translate="true"> interface, that also can validate the view model type. </text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">A view model can have one or more validators, so FluentValidationProvider aggregates all validators in a single one using </text><text styleclass="Normal" style="font-style:italic;" translate="true">CompositeValidator</text><text styleclass="Normal" translate="true"> class. For performance reasons </text><text styleclass="Normal" style="font-style:italic;" translate="true">FluentValidationProvider </text><text styleclass="Normal" translate="true">only searches for validators on the assembly which the view model belongs to. </text></para>
    <para styleclass="Notes"><text styleclass="Notes" translate="true">FluentValidationProvider do not use NamingConventions</text></para>
    <para styleclass="Notes"><text styleclass="Notes" translate="true">FluentValidation is only available in NET40, NET45, Silverlight 4 and Silverlight 5</text></para>
  </body>
</topic>
